package com.changfen;

import java.util.HashMap;
import java.util.Map;

/**
 * @author huyapeng
 * @date 2019/9/5
 * Email: yapeng.hu@things-matrix.com
 */
public enum ResultCode {
    OK(0, "response.0", "ok"),
    NOT_FOUND(404, "response.404", "record not found"),


    SERVICE_UNAVAILABLE(503, "response.503", "The {%s} service is not available, please try again later"),

    ;
    ResultCode(Integer code, String msgId, String defaultMsg) {
        this.code = code;
        this.messageId = msgId;
        this.message = defaultMsg;
    }


    private static final Map<Integer, ResultCode> RESULT_CODE_MAP = new HashMap<>();

    static {
        ResultCode[] values = ResultCode.values();
        for (ResultCode value : values) {
            RESULT_CODE_MAP.put(value.getCode(), value);
        }
    }


    public static ResultCode valueOf(Integer code) {
        return RESULT_CODE_MAP.get(code);
    }

    public boolean containsCode(Integer code) {
        return RESULT_CODE_MAP.containsKey(code);
    }

    /**
     * 状态编码
     */
    private Integer code;
    /**
     * 状态默认信息
     */
    private String message;
    /**
     * 消息模板Id，用于国际化的时候将默认信息通过模板信息转换
     */
    private String messageId;

    public Integer getCode() {
        return code;
    }

    public String getMessage() {
        return message;
    }

    public String getMessageId() {
        return messageId;
    }
}
